package policy;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-07
 * Time: 23:32
 */

//演示拒绝策略CallerRunsPolicy: 调用任务的run()方法绕过线程池直接执行。
public class MyThreadPoolDemo7 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(
                1,
                2,
                2,
                TimeUnit.HOURS,
                new ArrayBlockingQueue<>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        //根据参数线程最多有15个，这里测试有16个任务
        for(int i=1;i<=10;i++) {
           int y=i;
           threadPoolExecutor.submit(()->{
               System.out.println(Thread.currentThread().getName()+"---"+y);
           });
        }
        threadPoolExecutor.shutdown();
        
        //最多可以执行3条线程，但是有10条线程，这个策略可以绕过run方法的线程，直接交给main线程
    }
    
}
